<?php
/**
 * The model file of common module of ZenTaoPHP.
 *
 * The author disclaims copyright to this source code.  In place of
 * a legal notice, here is a blessing:
 * 
 *  May you do good and not evil.
 *  May you find forgiveness for yourself and forgive others.
 *  May you share freely, never taking more than you give.
 */
class commonModel extends model
{
    /**
     * The construct function.
     * 
     * @access public
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        if(!defined('FIRST_RUN'))
        {
            if(!defined('SESSION_STARTED')) $this->startSession();
            $this->sendHeader();
            define('FIRST_RUN', true);
        }
    }

    /**
     * Start the session.
     * 
     * @access public
     * @return void
     */
    public function startSession()
    {
        session_name($this->config->sessionVar);
        if(isset($_GET[$this->config->sessionVar])) session_id($_GET[$this->config->sessionVar]);
        session_start();
    }

    /**
     * Set the header info.
     * 
     * @access public
     * @return void
     */
    public function sendHeader()
    {
        header("Content-Type: text/html; Language={$this->config->charset}");
        header("Cache-control: private");
    }

    public function printRunInfo($startTime)
    {
        $info['timeUsed'] = round(getTime() - $startTime, 4) * 1000;
        $info['memory']   = round(memory_get_peak_usage() / 1024, 1);
        $info['querys']   = count(dao::$querys);
        vprintf($this->lang->runInfo, $info);
        return $info;
    }

    public static function getModuleMenu($module = 'main', $rebuild = false)
    {
        if(empty($module)) $module = 'main';

        global $lang;
        $allMenu = $module == 'main' ? $lang->menu : (isset($lang->$module->menu) ? $lang->$module->menu : '');

        return $allMenu;
    }
    public static function getMainMenu($rebuild = false)
    {
        return self::getModuleMenu('main', $rebuild);
    }

    public static function printMainmenu($moduleName, $methodName = '')
    {
        /* Set the main main menu. */
        $mainMenu = $moduleName;
        /* Print all main menus. */

        $menu = self::getMainMenu();
        $activeName = 'active';
        echo "<ul class='nav'>\n";
        foreach($menu as $module=>$menuItem)
        {
            if(strpos($menuItem, '|') !== false)
            {
                $menuItem = explode('|', $menuItem);
                list($label, $module, $method) = $menuItem;
            }

            $active = $module == $mainMenu ? "class='$activeName'" : '';
            $link = helper::createLink($module, $method);

            echo "<li $active id='$module' data-id='$module'><a href='$link' $active>$label</a></li>\n";
        }
        echo "</ul>\n";
    }

    public static function printModuleMenu($moduleName){
        global $app;
        $menu = self::getModuleMenu($moduleName);
        $activeName = 'active';
        foreach($menu as $method=>$menuItem)
        {
            if(strpos($menuItem, '|') !== false)
            {
                $menuItem = explode('|', $menuItem);
                list($label, $module, $method) = $menuItem;
            }

            $active = $method == $app->methodName ? "class='$activeName'" : '';
            $link = helper::createLink($module, $method);
            echo "<li $active data-id='$method'><a href='$link' $active>$label</a></li>\n";
        }
    }



}
